AddPrefabPostInit(
    "world",
    function(inst)

        if not TheWorld.ismastersim then
            return
        end

        if inst.components.npc_base_lib == nil then
            inst:AddComponent("npc_base_lib")
        end
        if inst.components.npc_everything_data == nil then
            inst:AddComponent("npc_everything_data")
        end

        

        inst:DoTaskInTime(1,function()
            ------------------------------------------------------------------------------------------------------------------------------------
            inst:WatchWorldState("cycles", function()
                inst.npc_everything_data:Add("day",1)
            end)
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 小鱼人箱子事件
                local function Spawn_npc_item_wurt_hiding_box(inst,cycles_flag)
                    local npc_item_wurt_hiding_box = TheSim:FindFirstEntityWithTag("npc_item_wurt_hiding_box")
                    if npc_item_wurt_hiding_box == nil then
                        local pt = inst.npc_base_lib:GetRandomPoint(inst)
                        if cycles_flag then
                            local player = TheSim:FindFirstEntityWithTag("player")
                            if player then
                                pt = inst.npc_base_lib:GetSpawnPoint(player,25) or pt
                            end
                        end
                        if pt and pt.x then
                            npc_item_wurt_hiding_box = SpawnPrefab("npc_item_wurt_hiding_box")
                            npc_item_wurt_hiding_box.Transform:SetPosition(pt.x,0, pt.z)
                        end
                    end
                end

                if inst.npc_everything_data:Get("first_.npc_item_wurt_hiding_box") ~= true then
                    inst.npc_everything_data:Set("first_.npc_item_wurt_hiding_box",true)
                    Spawn_npc_item_wurt_hiding_box(inst)
                end
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 魔术礼帽送一个,地上地下各一个
                if inst.npc_everything_data:Get("first_tophat_gift") ~= true then
                    inst.npc_everything_data:Set("first_tophat_gift",true)
                    local pt = inst.npc_base_lib:GetRandomPoint(inst)
                    if pt and pt.x then
                        SpawnPrefab("npc_item_magic_tophat").Transform:SetPosition(pt.x, 0,pt.z)
                        SpawnPrefab("npc_fx_tophat_first_gift_fx").Transform:SetPosition(pt.x, 0,pt.z)
                    end
                end
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 沃拓克斯传送门
                local function Spawn_Wortox_Portal(inst,cycles_flag)
                    local npc_item_red_portal = TheSim:FindFirstEntityWithTag("npc_item_red_portal")
                    if npc_item_red_portal == nil then
                        local pt = inst.npc_base_lib:GetRandomPoint(inst)

                        if cycles_flag then
                            local player = TheSim:FindFirstEntityWithTag("player")
                            if player then
                                pt = inst.npc_base_lib:GetSpawnPoint(player,25) or pt
                            end
                        end

                        if pt and pt.x then
                            npc_item_red_portal = SpawnPrefab("npc_item_red_portal")
                            npc_item_red_portal.Transform:SetPosition(pt.x,0, pt.z)
                        end
                    end
                end

                if inst.npc_everything_data:Get("first_.npc_item_red_portal") ~= true then
                    inst.npc_everything_data:Set("first_.npc_item_red_portal",true)
                    Spawn_Wortox_Portal(inst)
                end
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 洞穴遇难者       
                local function Spawn_npc_item_risk_taker_tent(inst)
                    local npc_item_risk_taker_tent = TheSim:FindFirstEntityWithTag("npc_item_risk_taker_tent")
                    if npc_item_risk_taker_tent == nil then
                        npc_item_risk_taker_tent = SpawnPrefab("npc_item_risk_taker_tent")
                        npc_item_risk_taker_tent:PushEvent("Find_Location")
                    end
                end         

                if inst:HasTag("cave") then                    
                    if inst.npc_everything_data:Get("first_.npc_item_risk_taker_tent") ~= true then
                        inst.npc_everything_data:Set("first_.npc_item_risk_taker_tent",true)
                        Spawn_npc_item_risk_taker_tent(inst)
                    end
                end
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 刷骷髅                
                local function Spawn_Skeleton(inst)
                    local player = TheSim:FindFirstEntityWithTag("player")
                    if player and player.components.playercontroller then
                        local temp_pt = Vector3(player.Transform:GetWorldPosition())                        
                        local pt = player.npc_base_lib:GetSpawnPoint(player,30)
                        if pt  and temp_pt.y == 0 then
                            local skeleton = SpawnPrefab("skeleton")
                            if skeleton then
                                skeleton.Transform:SetPosition(pt.x,0, pt.z)
                                skeleton:PushEvent("skeleton.ready")
                            end
                        end
                    end
                end
            ------------------------------------------------------------------------------------------------------------------------------------
            ------------------------------------------------------------------------------------------------------------------------------------
            ------------------------------------------------------------------------------------------------------------------------------------
            inst:WatchWorldState("cycles", function()   ----------------------- 循环总入口
                local day = inst.npc_everything_data:Add("day",0)

                if day%50 == 0 then Spawn_npc_item_wurt_hiding_box(inst,true) end   ------ 小鱼人箱子事件

                if day%40 == 0 then Spawn_Wortox_Portal(inst,true) end              ------ 沃拓克斯传送门

                if inst:HasTag("cave") and day%40 == 0 then Spawn_npc_item_risk_taker_tent(inst) end    --- 洞穴遇难者

                -- if TUNING.NPC_MOD_THESIM_HOOKED == true then            ------ 骷髅在玩家附近
                --     if day == 0 or day%30 == 0 then Spawn_Skeleton(inst) end       
                -- else
                --     if day%60 == 0 then Spawn_Skeleton(inst) end
                -- end
                if day%60 == 0 and TUNING.NPC_CONFIG.SKELETON_EVENT then Spawn_Skeleton(inst) end  ------ 骷髅在玩家附近


            end)
            ------------------------------------------------------------------------------------------------------------------------------------
            ------- 一次性事件：被冻住的NPC
                if inst:HasTag("cave") and inst.npc_everything_data:Get("npc_item_frozen_npc.first") ~= true and TUNING.NPC_CONFIG.CAVE_ICE_SCULPTURE == true then
                    inst.npc_everything_data:Set("npc_item_frozen_npc.first",true)

                    local cap = TheSim:FindFirstEntityWithTag("npc_tag.toadstool_cap")
                    if cap then
                        local pt = inst.npc_base_lib:GetSpawnPoint(cap,5)
                        if pt and pt.x then
                            SpawnPrefab("npc_item_frozen_npc").Transform:SetPosition(pt.x, 0, pt.z)
                        end
                    end
                    
                end
            ------------------------------------------------------------------------------------------------------------------------------------
        end)

    end
)

-------------------------------------------------------------------------------------------
---- woodlegs_out_of_cage.task_end
---- 海盗船长逃离笼子相关代码配置
AddPrefabPostInit(
    "world",
    function(inst)
        if not TheWorld.ismastersim then
            return
        end

        if inst.components.npc_base_lib == nil then
			inst:AddComponent("npc_base_lib")
		end
		if inst.components.npc_everything_data == nil then
			inst:AddComponent("npc_everything_data")
		end

        if inst.npc_everything_data:Get("woodlegs_out_of_cage.task_end") ~= true then
            inst:ListenForEvent("woodlegs_out_of_cage.task_end",function()
                inst.npc_everything_data:Set("woodlegs_out_of_cage.task_end",true)
            end)

        end

        inst:DoTaskInTime(2,function()
            ------------------------------------------------------------------------------
            inst:WatchWorldState("cycles", function()
                if not inst:HasTag("cave") and inst.npc_everything_data:Get("woodlegs_out_of_cage.task_end") == true then
                    ------- 在洞外，且救出人了
                    local tent = TheSim:FindFirstEntityWithTag("npc_item_woodlegs_tent")

                    ---------------------------------------------------------------------------------------------------------------
                    ---- 如果没有帐篷，刷一个，并放到绚丽之门附近
                        if tent == nil then 
                            tent = SpawnPrefab("npc_item_woodlegs_tent_item")
                            local door = TheSim:FindFirstEntityWithTag("multiplayer_portal")
                            if door then
                                local pt = door.npc_base_lib:GetSpawnPoint(door,10) or Vector3(door.Transform:GetWorldPosition())
                                tent.Transform:SetPosition(pt.x,0, pt.z)
                                tent:PushEvent("Back_To_Door")
                            end
                        end
                    ---------------------------------------------------------------------------------------------------------------
                    ---- 如果3天都处于物品状态,且不在绚丽之门附近,且不在玩家身上，放回绚丽之门附近
                        if tent and tent:HasTag("item") then
                            print("woodlegs' tent is item ,day:",inst.npc_everything_data:Add("npc_item_woodlegs_tent.item_day",0))
                            local item_pt = Vector3(tent.Transform:GetWorldPosition())
                            if #TheSim:FindEntities(item_pt.x,0, item_pt.z, 15, {"multiplayer_portal"}, nil, nil) == 0 
                                and inst.npc_everything_data:Add("npc_item_woodlegs_tent.item_day",1) > 3 
                                and tent.npc_base_lib:Get_Owner() == nil
                            then
                                local door = TheSim:FindFirstEntityWithTag("multiplayer_portal")
                                if door then
                                    local pt = door.npc_base_lib:GetSpawnPoint(door,10) or Vector3(door.Transform:GetWorldPosition())
                                    tent.Transform:SetPosition(pt.x,0, pt.z)
                                end
                                tent:PushEvent("Back_To_Door")
                            end
                        end
                    ---------------------------------------------------------------------------------------------------------------

                end
            end)
            ------------------------------------------------------------------------------
        end)
        
        ----------------------------     

    end
)